


local item_max = 50
--词条星级	词条上限	单词条几率	品质影响    分解想强化石
local tz_list = {
    ['T1'] = {1,1,5,1,1},
    ['T2'] = {2,2,8,1,2},
    ['T3'] = {3,3,10,1,3},
}

--难度 名称  权重
local data_list = {
    ['T1-硬皮护手'] = {1,50,'护手'},
    ['T1-硬皮战靴'] = {1,50,'靴子'},
    ['T1-青铜战盔'] = {2,50,'头盔'},
    ['T1-革质胸甲'] = {2,50,'胸甲'},
    ['T1-莹石宝剑'] = {3,70,'武器'},
    ['T1-树人'] = {3,10,'宠物'},
    ['T1-狼灵'] = {3,10,'宠物'},
    ['T1-小冰龙'] = {3,10,'宠物'},
    ['T2-焰钢护手'] = {4,50,'护手'},
    ['T2-焰钢靴子'] = {4,50,'靴子'},
    ['T2-焰钢头盔'] = {5,50,'头盔'},
    ['T2-焰钢胸甲'] = {5,50,'胸甲'},
    ['T2-光咒宝剑'] = {6,70,'武器'},
    ['T2-树人'] = {6,10,'宠物'},
    ['T2-狼灵'] = {6,10,'宠物'},
    ['T2-小冰龙'] = {6,10,'宠物'},
    ['T3-龙鳞护手'] = {7,50,'护手'},
    ['T3-龙鳞靴子'] = {7,50,'靴子'},
    ['T3-龙鳞头盔'] = {8,50,'头盔'},
    ['T3-龙鳞胸甲'] = {8,50,'胸甲'},
    ['T3-心徽宝剑'] = {9,70,'武器'},
    ['T3-树人'] = {9,10,'宠物'},
    ['T3-狼灵'] = {9,10,'宠物'},
    ['T3-小冰龙'] = {9,10,'宠物'},
}
--权重表
local weight_list = {}
for name,da in pairs(data_list) do
    local hard = da[1]
    local odds = da[2]
    if weight_list[hard]==nil then
        weight_list[hard] = {}
    end
    local tz = name:match('(T%d)-(.*)')
    da[4] = tz
    
    table.insert(weight_list[hard],{name,odds})
end

local abi_list = {
    [1] = 15,
    [2] = 17,
    [3] = 8,
}

local get_item_data = function(key,hard)
    local item = {}
    local list = weight_list[hard] or weight_list[#weight_list]
    local rdm = randomGroup(key,10)  --用ID做种子产生10个随机数
    local name = generateWeight(rdm[1],list)
    local data = data_list[name]
    item.name = name
    item.class = data[3]
    item.tz = data[4]
    item.pz = generateRandom(rdm[2],1,4)
    return item
end

local random_affix = function(tz,pz)
    local tzdata = tz_list[tz]
    local affix_maxxx = tzdata[1]
    local affix_count = tzdata[2]
    local affix_odds = tzdata[3]
    local affix_odds2 = tzdata[4]

    local odds = (affix_odds + (pz - 1) * affix_odds2)
    local affix = {}
    for a=1,affix_count do
        local rdm = math.random(100)
        if rdm<=odds then
            local xx = math.ceil(math.random(1,100) / 100 * affix_maxxx)
            local id = math.ceil(math.random(1,1000) / 1000 * abi_list[xx])
            table.insert(affix,xx*1000 + id)
        end
    end
    return affix
end

function create_item(player,hard)
    local id = player:get_itemid()
    local key = ('存档装备[%s]'):format(id)
    local item = get_item_data(key,hard)
    
    item.id = id
    item.time = os.time()
    item.affix = random_affix(item.tz,item.pz)
    
    return item
end


--新版装备产出
function create_file_item(player,hard)
    local index = player:get_itemid()
    local list = weight_list[hard] or weight_list[#weight_list]
    local name = table.get_by_weight(list)
    local data = data_list[name]
    local ty = data[3]
    local tz = data[4]
    local tzdata = tz_list[tz]
    local affix_maxxx = tzdata[1]
    local affix_count = tzdata[2]
    local affix_odds = tzdata[3]
    local affix_odds2 = tzdata[4]
    local price = tzdata[5]
    local pz = math.random(4)
    local time = os.time()
    local info = {}
    
    local odds = (affix_odds + (pz - 1) * affix_odds2)
    local affix = {}
    for a=1,affix_count do
        local rdm = math.random(100)
        if rdm<=odds then
            local xx = math.ceil(math.random(1,100) / 100 * affix_maxxx)
            local id = math.ceil(math.random(1,1000) / 1000 * abi_list[xx])
            table.insert(affix,xx*1000 + id)
        end
    end
    info.id = index
    info.name = name
    info.level = 0
    info.pz = pz
    info.tz = tz
    info.abi = affix
    info.price = price
    info.class = ty
    info.time = time
    return info
end

--宝石产出
function create_bs_item(player,id)
    local index = player:get_itemid()
    local info = {}
    info.name = '词条宝石'
    info.class = '宝石'
    info.id = index
    info.abi = id
    return info
end

--通过id查找装备
function ac.player.__index:operate_item(id,func)
    local list = self:get('存档装备-背包')
    for a,item in ipairs(list) do
        if item.id==id then
            if func then
                func(a,item,list)
            end
            return item
        end
    end
end

--通过id查找装备
function ac.player.__index:get_itemid()
    local index = self:get('存档装备生成数量') + 1
    self:set('存档装备生成数量',index)
    return index
end


--添加物品
function ac.player.__index:add_item(item)
    local list = self:get('存档装备-顺序')
    local data = self:get('存档装备-背包')
    local slotid = nil
    if #data>=item_max then
        return
    end
    for a=1,item_max do
        if list[a]==0 then
            list[a] = item.id
            slotid = a
            break
        end
    end
    data[#data + 1] = item
    if self:get_data('初始化存档装备') then
        self:sendWar3('获得存档装备',item,slotid)
    end
end

--删除物品
function ac.player.__index:del_item(item,func)
    local item_id = item.id
    local data1 = self:get('存档装备-背包')
    local data2 = self:get('存档装备-顺序')
    self:operate_item(item_id,function(a,item,list)
        table.remove(list,a)
        for a,id in pairs(data2) do
            if id==item_id then
                data2[a] = 0
                break
            end
        end
        self:sendWar3('移除存档装备',item_id)
    end)
end

--[获得]
ac.allowEvents['获得存档装备'] = true
ac.game:event '玩家-获得存档装备'(function(_,player,hard)
    print(player:get_data('存档装备',0)<3 and ac.hard )
    if player:get_data('存档装备',0)<3 and ac.hard then
        local hard = hard or 1
        local item = create_file_item(player,hard)
        player:add_item(item)
        player:set_data('存档装备',player:get_data('存档装备',0) + 1)
    end
end)

--[分解]
ac.allowEvents['分解存档装备'] = true
ac.game:event '玩家-分解存档装备'(function(_,player,id)
    local list = player:get('存档装备-顺序')
    player:operate_item(id,function(a,item,data)
        player:add('强化石',item.price or 0)
        player:del_item(item)
        player:sendWar3('更新资源','强化石',player:get('强化石'))
        local affix = item.abi
        for a,id in ipairs(affix) do
            if math.random(100)<=100 then
                local bs = create_bs_item(player,id)
                player:add_item(bs)
            end
        end
    end)
end)


--[镶嵌]
ac.allowEvents['镶嵌存档宝石'] = true
ac.game:event '玩家-镶嵌存档宝石'(function(_,player,id1,id2)
    local item1 = player:operate_item(id1)
    local item2 = player:operate_item(id2)

    if item1 and item2 then
        if item1.name~='词条宝石' and item2.name=='词条宝石' and #item1.abi<3 then
            local affix = item1.abi
            for a,id in ipairs(affix) do
                if id==item2.abi then
                    return
                end
            end
            table.insert(affix,item2.abi)
            player:del_item(item2)
            player:sendWar3('获得存档装备',item1)
            player:sendWar3('装备处理消息','镶嵌成功',item1.id)
        end
    end
end)

--[强化]
ac.allowEvents['强化存档装备'] = true
ac.game:event '玩家-强化存档装备'(function(_,player,id)

end)

--[取下]
ac.allowEvents['取下存档装备'] = true
ac.game:event '玩家-取下存档装备'(function(_,player,id)
    local data2 = player:get('存档装备-携带')
    for key,item in pairs(data2) do
        if item.id==id then
            data2[key] = nil
            return
        end
    end
end)

--[佩戴]
ac.allowEvents['佩戴存档装备'] = true
ac.game:event '玩家-佩戴存档装备'(function(_,player,id)
    local data1 = player:get('存档装备-背包')
    local data2 = player:get('存档装备-携带')
    player:operate_item(id,function(a,item,list)
        local class = item.class
        data2[class] = id
    end)
end)

--[移动]
ac.allowEvents['拖动存档装备'] = true
ac.game:event '玩家-拖动存档装备'(function(_,player,id1,pos)
    local list = player:get('存档装备-顺序')
    if pos>item_max then
        return
    end
    
    local old = list[id1]
    local new = list[pos]
    list[pos] = old
    list[id1] = new
end)

ac.allowEvents['获取存档背包'] = true
ac.game:event '玩家-获取存档背包'(function(_,player)
    local data0 = player:get('存档装备-顺序') or {}
    local data1 = player:get('存档装备-背包') or {}
    local data2 = player:get('存档装备-携带') or {}
    local qhs = player:get('强化石') or 0
    player:sendWar3('获取存档背包',data1,data2,data0,qhs)
    player:set_data('初始化存档装备',true)
end)



ac.game:event '玩家-初始化数据'(function(_,player)
    local data0 = player:get('存档装备-顺序') or {}
    local data1 = player:get('存档装备-背包') or {}
    local data2 = player:get('存档装备-携带') or {}
    local maps = {}
    for _,info in ipairs(data1) do
        maps[info.id] = true
    end
    for a=1,item_max do
        local id = data0[a] or 0
        data0[a] = id
        maps[id] = nil
    end
    for id in pairs(maps) do
        for a=1,item_max do
            if data0[a]==0 then
                data0[a] = id
                break
            end
        end
    end
    player:set('存档装备-顺序',data0)
    player:set('存档装备-背包',data1)
    player:set('存档装备-携带',data2)
    player:set('强化石',player:get('强化石') or 0)
    player:set('存档装备生成数量',player:get('存档装备生成数量') or 0)
    player:notify('玩家-获取存档背包',player)
end)